package ch.nortgram.countdown_worldcup_2014.fragment;

import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import ch.nortgram.countdown_worldcup_2014.R;

public class CountdownFragment extends Fragment
	{
	CountDownTimer wm2014_countdown_timer;
	TextView countdown_days;
	TextView countdown_hours;
	TextView countdown_minutes;
	TextView countdown_seconds;
	View countdown_view;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
		{
		countdown_view = inflater.inflate(R.layout.activity_countdown, container, false);
		
		countdown_days = (TextView)countdown_view.findViewById(R.id.txt_countdown_days);
		countdown_hours = (TextView)countdown_view.findViewById(R.id.txt_countdown_hours);
		countdown_minutes = (TextView)countdown_view.findViewById(R.id.txt_countdown_minutes);
		countdown_seconds = (TextView)countdown_view.findViewById(R.id.txt_countdown_seconds);
		// description
		
		
		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"roboto_bold.ttf");
		countdown_days.setTypeface(tf);
		countdown_hours.setTypeface(tf);
		countdown_minutes.setTypeface(tf);
		countdown_seconds.setTypeface(tf);
		return countdown_view;
		}
    	
	private void countdown_start()
		{
		long timestamp_end = 1402592400;
		long timestamp_now = System.currentTimeMillis() / 1000;
		timestamp_now = timestamp_now - TimeZone.getDefault().getOffset(timestamp_now) / 1000;
		
    	wm2014_countdown_timer = new CountDownTimer((timestamp_end - (timestamp_now)) *1000, 500)
    		{
			@Override
			public void onFinish()
				{
	            //action to do when the timer has finished
	            }
	
			@Override
	    	public void onTick(long millisUntilFinished)
				{
				int days = (int) TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
				millisUntilFinished -= TimeUnit.DAYS.toMillis(days);
		        int hours = (int) TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
		        millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);
		        int minutes = (int) TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
		        millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);
		        int seconds = (int) TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
		        
		        update_countdown(days,hours,minutes,seconds);
		       }

			private void update_countdown(int days, int hours, int minutes, int seconds)
				{
				countdown_days.setText(String.format("%03d", days));
				countdown_hours.setText(String.format("%02d", hours));
				countdown_minutes.setText(String.format("%02d", minutes));
				countdown_seconds.setText(String.format("%02d", seconds));
				}
    		};
    	wm2014_countdown_timer.start();
		}
	
	
	@Override
	public void onPause()
		{
		super.onPause();
		Log.e("","paused timer");
    	wm2014_countdown_timer.cancel();
		wm2014_countdown_timer = null;
		}
	
	@Override
	public void onResume()
		{
		super.onResume();
		Log.e("","started timer");
		countdown_start();
		}
	}